package com.example.actionbardemo.tabandviewpager;

import java.util.ArrayList;
import java.util.List;

import com.example.actionbardemo.FuncListActivity;
import com.example.actionbardemo.R;
import com.example.actionbardemo.fragment.BaseFragment;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;

public class TabViewPagerActivity extends FuncListActivity implements ActionBar.TabListener,OnPageChangeListener {
	/**Fragment集合*/
	private List<Fragment> fragments = new ArrayList<Fragment>(); 
	private List<Tab> tabs = new ArrayList<Tab>();
	/**Fragment适配器*/
	private FragmentPagerAdapter adapter;
	private ActionBar actionBar;
	private ViewPager vp;

	@Override
	public int getContentViewID() {
		return R.layout.activity_tabandviewpager;
	}
	
	@Override
	public boolean isBackIconVisible() {
		return true;
	}
	
	@Override
	public void initData() {
		String[] items = getResources().getStringArray(R.array.nav_list);
		actionBar = getActionBar();  
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);// tab导航模式必须设为NAVIGATION_MODE_Tabs 
		for(int i=0;i<items.length;i++){
			BaseFragment fragment = new BaseFragment(items[i]);
			fragments.add(fragment);
			
			//处理ActionBar的tab项.
			ActionBar.Tab tab = actionBar.newTab();
        	tab.setText(items[i]);		//设置tab的标题.
        	tab.setTabListener(this);   //设置tab点击的监听.
        	tabs.add(tab);  			//将tab实例存储到集合.
        	actionBar.addTab(tab);		//将tab添加到actionBar.
		}
		
		adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragments);
	}
	
	@Override
	public void initView() {
		vp = (ViewPager) findViewById(R.id.vp);
		vp.setAdapter(adapter);
		vp.setOnPageChangeListener(this);
	}
	
	
	/**
	 * 适配器类
	 * @author yuanchao
	 */
	private class MyFragmentPagerAdapter extends FragmentPagerAdapter {
		private final List<Fragment> list;

		public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {
			super(fm);
			this.list = list;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Fragment getItem(int position) {
			return list.get(position);
		}
	}


	//---------------------------------------TabListener需实现的方法-------------------------------------
	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		int position = tab.getPosition();
		if(vp!=null){ //这个判断必须得有,因为在tab设置监听时,vp都还没有实例化是null.
			vp.setCurrentItem(position);
		}
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		
	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		
	}

	//-----------------------------------OnPageChangeListener需实现的方法--------------------------------
	@Override
	public void onPageScrollStateChanged(int arg0) {
		
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		
	}

	@Override
	public void onPageSelected(int position) {
		Tab tab = tabs.get(position);
		tab.select();
	}

}
